﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarsModel;
using CarsDal;

namespace CarsBll
{
    public class UsersBll
    {
        UsersDal dal = new UsersDal();

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns name="0">用户不存在</returns>
        /// <returns name="1">手机号错误</returns>
        /// <returns name="2">登录成功</returns>

        public int  Login(string UsersNameorTel,string UsersPwd)
        {
            List<Users> UsersList = dal.LoginList();

            Users usermodel = UsersList.Where(p => p.UsersPwd == UsersNameorTel || p.UsersTel == UsersNameorTel).FirstOrDefault();


            if (usermodel == null)
            {
                //"用户不存在";
                return -1; 
            }
            else if (usermodel.UsersPwd != UsersPwd)
            {
                //"密码错误";
                return -2;
            }
            else
            {
                if (usermodel.UsersStateID == 1)
                {
                    //"该用户已登录";
                    return -3;
                }
                else
                {
                    dal.UsersUpdateLoginState(1,usermodel.UsersID); 
                    //"登录成功";
                    return usermodel.UsersID;
                }
            }
            
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="UsersNameorTel">用户电话或用户名</param>
        /// <param name="UsersPwd">密码</param>
        /// <returns name="0">该账户已存在</returns>
        /// <returns name="1">注册成功</returns>
        /// <returns name="2">注册失败</returns>
        public string UsersRegist(string UsersNameorTel, string UsersPwd)
        {
            List<Users> UsersList = dal.LoginList();

            Users usermodel = UsersList.Where(p => p.UsersPwd == UsersNameorTel || p.UsersTel == UsersNameorTel).FirstOrDefault();
            if (usermodel != null)
            {
                return "该用户已经存在，请重新注册";
            }
            else
            {
                int i=dal.UsersRegist(UsersNameorTel,UsersPwd);
                if (i == 1)
                {
                    return "注册成功";
                }
                else
                {
                    return "注册失败";
                }
            }
            

        }

        /// <summary>
        /// 用户安全退出
        /// </summary>
        /// <returns></returns>
        public int UsersExit(int UsersID)
        {
            int i=dal.UsersUpdateLoginState(0,UsersID);
            return i;
        }


        /// <summary>
        /// 用户完善信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string  UsersInfoUpdate(Users UsersModel)
        {
            Users model = dal.LoginList().Where(p => p.UsersID == UsersModel.UsersID).FirstOrDefault();
            UsersModel.UsersTel = model.UsersTel;
            int i = dal.UsersUpdateInfo(model);

            if (i > 0)
            {
                return "修改成功";
            }
            else
            {
                return "修改失败";
            }
        }
    }
}
